/* Andreas Baum
 * ChatClientPanel.java
 * Handles the display of the applet for client-server
 * interaction
 */

package ChatClientApplet;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.util.Arrays;

import javax.swing.*;

public class ChatClientPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel window;
	private JLabel mainTitle;
	private JLabel contactsTitle;
	private JTextArea contactsWindow;
	private JLabel outputTitle;
	private JTextArea outputWindow;
	private JTextArea inputTitle;
	private JTextField inputField;
	private JScrollPane outputPane;
	private JLabel PMTitle;
	private JTextArea PMWindow;
	private JTextArea PMInputTitle;
	private JTextField PMInputField;
	private JScrollPane PMPane;
	private String username;

	Socket socket;

	private AppletThread appletThread; 

	public ChatClientPanel(Socket socketArg, String user) {
		socket = socketArg;
		username = user;
		init();
	}


	public void init() {
		window = new JPanel(new GridBagLayout()); 
		window.setBackground(Color.white);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 10;

		mainTitle = new JLabel("CHATROOM");
		mainTitle.setAlignmentX(CENTER_ALIGNMENT);
		constraints.gridwidth = 3;
		constraints.gridx = 0;
		constraints.gridy = 0;
		window.add(mainTitle, constraints);

		contactsTitle = new JLabel("Members");
		constraints.gridwidth = 1;
		constraints.gridx = 0;
		constraints.gridy = 1;
		window.add(contactsTitle, constraints);


		contactsWindow = new JTextArea("[admin]",30,8);
		contactsWindow.setEditable(false);
		contactsWindow.setFont(new Font("Courier", Font.PLAIN, 12));
		contactsWindow.setBorder(BorderFactory.createLineBorder(Color.black));
		//constraints.ipady = 400;
		constraints.gridx = 0;
		constraints.gridy = 2;
		window.add(contactsWindow, constraints);

		outputTitle = new JLabel("Main Chat");
		constraints.ipady = 0;
		constraints.gridwidth = 2;
		constraints.gridx = 1;
		constraints.gridy = 1;
		window.add(outputTitle, constraints);

		outputWindow = new JTextArea("\tWelcome to the Ch@tRoom\n",30,70);
		outputWindow.setFont(new Font("Courier", Font.PLAIN, 12));
		outputWindow.append("\n\"I love talking about nothing. It is the "
				+ "only thing I know anything about.\"");
		outputWindow.append("\t\t- Oscar Wilde\n");
		//outputWindow.setBorder(BorderFactory.createLineBorder(Color.black));
		outputWindow.setEditable(false);
		outputWindow.setLineWrap(true);
		outputWindow.setWrapStyleWord(true);

		outputPane = new JScrollPane(outputWindow);
		outputPane.setBorder(BorderFactory.createLineBorder(Color.black));
		//constraints.ipady = 400;
		constraints.gridx = 1;
		constraints.gridy = 2;
		window.add(outputPane, constraints);

		inputTitle = new JTextArea("Type:",1,5);
		inputTitle.setEditable(false);
		constraints.ipady = 0;
		constraints.gridwidth = 1;
		constraints.gridx = 1;
		constraints.gridy = 3;
		window.add(inputTitle, constraints);

		inputField = new JTextField("<Enter your chats here>");
		inputField.setColumns(35);
		inputField.addActionListener(this);
		inputField.setActionCommand("1");
		constraints.gridx = 2;
		constraints.gridy = 3;
		window.add(inputField, constraints);
		add(window);

		PMTitle = new JLabel("Private Messages");
		constraints.ipady = 0;
		constraints.gridwidth = 2;
		constraints.gridx = 1;
		constraints.gridy = 4;
		window.add(PMTitle, constraints);

		PMWindow = new JTextArea("",10,70);
		PMWindow.setFont(new Font("Courier", Font.PLAIN, 12));
		PMWindow.setEditable(false);
		PMWindow.setLineWrap(true);
		PMWindow.setWrapStyleWord(true);

		PMPane = new JScrollPane(PMWindow);
		PMPane.setBorder(BorderFactory.createLineBorder(Color.black));
		//constraints.ipady = 400;
		constraints.gridx = 1;
		constraints.gridy = 5;
		window.add(PMPane, constraints);

		PMInputTitle = new JTextArea("Type:",1,5);
		PMInputTitle.setEditable(false);
		constraints.ipady = 0;
		constraints.gridwidth = 1;
		constraints.gridx = 1;
		constraints.gridy = 6;
		window.add(PMInputTitle, constraints);

		PMInputField = new JTextField("<Enter your private messages here>");
		PMInputField.setColumns(35);
		PMInputField.addActionListener(this);
		PMInputField.setActionCommand("2");
		constraints.gridx = 2;
		constraints.gridy = 6;
		window.add(PMInputField, constraints);

		add(window);

		appletThread = new AppletThread(socket, username, this);

	}

	public void actionPerformed(ActionEvent evt) {
		String text;
		switch (Integer.parseInt(evt.getActionCommand())) {
		case 1:
			text = inputField.getText();
			appletThread.send(text);
			inputField.selectAll();
			inputField.setText("");
			//Make sure the new text is visible, even if there
			//was a selection in the text area.
			outputWindow.setCaretPosition(outputWindow.getDocument().getLength());
			break;
		case 2:
			text = PMInputField.getText();
			appletThread.send("PM " + text);
			PMInputField.selectAll();
			PMInputField.setText("");
			PMWindow.setCaretPosition(PMWindow.getDocument().getLength());
			break;
		}
	}

	public void appendOutput(String string) {
		this.outputWindow.append("\n" + string);
		this.outputWindow.setCaretPosition(outputWindow.getDocument().getLength());
	}
	
	public void appendPM(String string) {
		this.PMWindow.append("\n" + string);
		this.PMWindow.setCaretPosition(PMWindow.getDocument().getLength());
	}

	public AppletThread getAppletThread() {
		return appletThread;
	}


	public void updateMembers(String users) {
		String[] userlist = users.split(" ");
		Arrays.sort(userlist);
		String members = "";
		int i;
		for (i=0;i<userlist.length;i++) {
			members = members + "\n" + userlist[i];
		}
		this.contactsWindow.setText(members);
		System.out.println("Setting text: " + members);
	}

}